import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

/**
 * Created with IntelliJ IDEA.
 * User: OzZiE
 * Date: 11/1/13
 * Time: 11:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class PennyStock extends JPanel
{
    //main panels declarations
    private JPanel leftTop = new JPanel(new GridBagLayout());

    //GUI components for left panel
    private JLabel comishLabel = new JLabel("How much is your stock broker's commission?");
    public JTextField comishField = new JTextField();
    private JLabel perShareLabel = new JLabel("How much does your broker charge per share?");
    public JTextField perShareField = new JTextField();
    private JLabel stockPriceLabel = new JLabel("What is the price of the stock?");
    public JTextField stockPriceField = new JTextField();
    private JLabel investingLabel = new JLabel("How much are you investing?");
    public JTextField investingField= new JTextField();
    private JLabel makeLabel = new JLabel("How much do you want to make?");
    public JTextField makeField= new JTextField();
    private JButton generate = new JButton("Generate");
    //private JButton clickMe = new JButton("Click Me");
    private JButton clear = new JButton("Clear");


    //grid bag constraints
    GridBagConstraints c = new GridBagConstraints();
    Insets m = new Insets(2,10,5,5);

    //constructor, set the main layout to GridLayout
    public PennyStock()
    {
        setLayout(new GridLayout(1, 0));

    /*    clickMe.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {

            }
        });      */

        generate.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try{
                double formatted;
                double formatted2;
                DecimalFormat threeDigits = new DecimalFormat("###0.000");

                double comish = Double.parseDouble(comishField.getText());
                double perShareFee = Double.parseDouble(perShareField.getText());
                double price = Double.parseDouble(stockPriceField.getText());
                double investing = Double.parseDouble(investingField.getText());
                double make = Double.parseDouble(makeField.getText());

                double stocksAbleToBuy = investing/price;

                double totalPerShareFee = (stocksAbleToBuy*perShareFee) * 2;
                double totalComishFee = comish*2;
                double totalFees = totalPerShareFee+totalComishFee;
                double gainNeeded = make + totalFees;  //the gain needed to make the profit the user wants
                double needIncreaseOf = gainNeeded/stocksAbleToBuy; //the total increase needed in the stock to make the requested profit

                formatted = Double.valueOf(threeDigits.format(needIncreaseOf));
                formatted2 = Double.valueOf(threeDigits.format(price+needIncreaseOf));

                JOptionPane.showMessageDialog(getRootPane(), "<html>You need an increase of about $" +formatted+ " to make your goal.<br>Sell your stock at $" +formatted2+ ".</html>", "Goal", JOptionPane.PLAIN_MESSAGE);

                } catch (Exception a){
                    JOptionPane.showMessageDialog(getRootPane(), "You must properly fill out all text fields", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        clear.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                comishField.setText(null);
                perShareField.setText(null);
                stockPriceField.setText(null);
                investingField.setText(null);
                makeField.setText(null);
            }
        });
    }

    //create main panel
    public void createPanel()
    {
        c.gridx = 0;
        c.gridy = 0;
        m = new Insets(10,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(comishLabel, c);

        c.gridx = 0;
        c.gridy = 1;
        c.ipadx = 100;
        m = new Insets(2,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(comishField, c);

        c.gridx = 0;
        c.gridy = 2;
        c.ipadx = 0;
        m = new Insets(10,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(perShareLabel, c);

        c.gridx = 0;
        c.gridy = 3;
        c.ipadx = 100;
        m = new Insets(2,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(perShareField, c);

        c.gridx = 0;
        c.gridy = 4;
        c.ipadx = 0;
        m = new Insets(10,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(stockPriceLabel, c);

        c.gridx = 0;
        c.gridy = 5;
        c.ipadx = 100;
        m = new Insets(2,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(stockPriceField, c);

        c.gridx = 0;
        c.gridy = 6;
        c.ipadx = 0;
        m = new Insets(10,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(investingLabel, c);

        c.gridx = 0;
        c.gridy = 7;
        c.ipadx = 100;
        m = new Insets(2,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(investingField, c);

        c.gridx = 0;
        c.gridy = 8;
        c.ipadx = 0;
        m = new Insets(10,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(makeLabel, c);

        c.gridx = 0;
        c.gridy = 9;
        c.ipadx = 100;
        m = new Insets(2,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(makeField, c);

     /*   c.gridx = 0;
        c.gridy = 10;
        c.ipadx = 0;
        m = new Insets(20,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(clickMe, c);   */

        c.gridx = 0;
        c.gridy = 10;
        c.ipadx = 0;
        m = new Insets(20,0,5,0);
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(generate, c);

        c.gridx = 1;
        c.gridy = 10;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(clear, c);

        add(leftTop);
    }
}
